<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @author daoquangphuong
 * @copyright 2013
 */
 
class Http
{
    Public $Curl;    
    public $Cookie_file;
    public function __construct() 
    {
        $this->Cookie_file="c:\\HttpCookie.txt";        
    }
    
    private function _new()
    {
        $this->Curl = curl_init();
        curl_setopt($this->Curl,CURLOPT_USERAGENT,'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; Tablet PC 2.0)');
        curl_setopt($this->Curl,CURLINFO_HEADER_OUT,true);
        curl_setopt ($this->Curl, CURLOPT_HEADER, 0);
        curl_setopt ($this->Curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($this->Curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($this->Curl, CURLOPT_FOLLOWLOCATION, 1);            
    }
    
    public function Get($_Url,$_Header='',$_Timeout=30,$_Cookie)
    {
        $this->_new();
        curl_setopt($this->Curl,CURLOPT_URL,$_Url);
        if ($_Header!=='')
        {
            curl_setopt($this->Curl,CURLOPT_HTTPHEADER,$_Header);
        }
        curl_setopt($this->Curl,CURLOPT_TIMEOUT,$_Timeout);
        
        if (isset($_Cookie))
        {
            $frw = fopen($this->Cookie_file, 'w'); 
            fwrite($frw, $_Cookie);
            fclose($frw);
            curl_setopt($this->Curl,CURLOPT_COOKIEFILE,$this->Cookie_file);
            curl_setopt($this->Curl,CURLOPT_COOKIEJAR,$this->Cookie_file);            
        }
        
        $string[0] = mb_convert_encoding(curl_exec ($this->Curl), "HTML-ENTITIES", "UTF-8");      
        $string[2] = curl_getinfo($this->Curl,CURLINFO_HEADER_OUT);       
        curl_close ($this->Curl);
        $frw = fopen($this->Cookie_file, 'r');       
        $string[1] = fread($frw, filesize($this->Cookie_file));;
        fclose($frw);
        unlink($this->Cookie_file);
        return $string;
    }
    
    public function Post($_Url,$Data,$_Header='',$_Timeout=30,$_Cookie)
    {
        $this->_new();
        curl_setopt($this->Curl,CURLOPT_URL,$_Url);
        curl_setopt ($this->Curl, CURLOPT_POST, 1);  
        curl_setopt ($this->Curl, CURLOPT_POSTFIELDS, $Data);
              
        if ($_Header!=='')
        {
            curl_setopt($this->Curl,CURLOPT_HTTPHEADER,$_Header);
        }
        curl_setopt($this->Curl,CURLOPT_TIMEOUT,$_Timeout);
        
        if (isset($_Cookie))
        {
            $frw = fopen($this->Cookie_file, 'w'); 
            fwrite($frw, $_Cookie);
            fclose($frw);
            curl_setopt($this->Curl,CURLOPT_COOKIEFILE,$this->Cookie_file);
            curl_setopt($this->Curl,CURLOPT_COOKIEJAR,$this->Cookie_file);            
        }
        
        $string[0] = mb_convert_encoding(curl_exec ($this->Curl), "HTML-ENTITIES", "UTF-8");      
        $string[2] = curl_getinfo($this->Curl,CURLINFO_HEADER_OUT);       
        curl_close ($this->Curl);
        $frw = fopen($this->Cookie_file, 'r');       
        $string[1] = fread($frw, filesize($this->Cookie_file));;
        fclose($frw);
        unlink($this->Cookie_file);
        return $string;
    }
}
?>